<template>
  <div class="ele-body">
    <!-- 搜索表单 -->

    <el-card shadow="never">
      <!-- 数据表格 -->
      <consumer-search @search="reload" />
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <template v-if="platform_id > 0" v-slot:toolbar>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
        </template>
        <template v-slot:consumer_type="{ row }">
          <ele-dot
            v-if="row.consumer_type == 1"
            type="warning"
            effect="plain"
            text="线上核销"
            :ripple="true"
          />
          <ele-dot
            v-if="row.consumer_type == 2"
            type="success"
            effect="plain"
            text="独立核销"
            :ripple="true"
          />
        </template>

<template v-slot:claim_number="{ row }">
          <div class="ele-cell" v-if="row.claim_number == 0">
            <div class="ele-cell-content">
              无限领取
            </div>
          </div>
          <span v-else>{{row.claim_number }}</span>
        </template>

        <template v-slot:platformInfo="{ row }">
          <div class="ele-cell" v-if="row.platformInfo">
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.platformInfo.name }} </div>
              <div class="ele-cell-desc">{{ row.platformInfo.telphone }} </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <template v-slot:satisfy_amount="{ row }">
          <div class="ele-cell" v-if="row.consumerInfo">
            <div class="ele-cell-content">
              <div class="ele-cell-title"
                >{{ row.consumerInfo.satisfy_amount }}
              </div>
              <div class="ele-cell-desc"
                >{{ row.consumerInfo.satisfy_amount }}
              </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <template v-slot:deductible_amount="{ row }">
          <div class="ele-cell" v-if="row.consumerInfo">
            <div class="ele-cell-content">
              <div class="ele-cell-title"
                >{{ row.consumerInfo.deductible_amount }}
              </div>
              <div class="ele-cell-desc"
                >{{ row.consumerInfo.deductible_amount }}
              </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <!-- 表头工具栏 -->
        <!-- 状态列 -->
        <template v-slot:status="{ row }">
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="row.status"
            @change="editStatus(row)"
          />
        </template>

     
        <consumer-edit
          :visible.sync="showEdit"
          :data="current"
          @done="reload"
        />
        <!-- 状态列 -->
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
  </div>
</template>

<script>
import {
  consumerLog,
  consumerLogRemove,
  modify,
  sortChange,
  doDefault
} from '@/api/consumer';
import consumerSearch from './components/consumer-search.vue';
import ConsumerEdit from './components/consumer-edit';
import Sortable from 'sortablejs';

export default {
  name: 'consumer',
  components: {
    consumerSearch,
    ConsumerEdit
  },
  created() {
    this.platform_id = localStorage.getItem('platform_id');
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },

        {
          prop: 'platformInfo',
          label: '平台详情',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'platformInfo',
          show: localStorage.getItem('platform_id') == 0 ? true : false
        },

        {
          prop: 'activityInfo.name',
          label: '活动名称',
          showOverflowTooltip: true,
          minWidth: 110
        },

        {
          prop: 'consumerInfo.satisfy_amount',
          label: '满足金额',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'consumerInfo.deductible_amount',
          label: '抵扣金额',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'claim_number',
          label: '发券数量',
          showOverflowTooltip: true,
          minWidth: 110,
             slot: 'claim_number',
        },

        {
          prop: 'claimed_number',
          label: '已领取数量',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'consumerInfo.store_undertake_proportion',
          label: '店铺承担比例',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'start_time',
          label: '开始时间',
          showOverflowTooltip: true,
          minWidth: 150
        },
        {
          prop: 'end_time',
          label: '结束时间',
          showOverflowTooltip: true,
          minWidth: 150
        },

    
        // {
        //   prop: 'status',
        //   label: '是否禁用',
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   minWidth: 110,
        //   slot: 'status'
        // }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示分配回收员
      showCollector: false,
      platform_id: '',

      //弹框名称
      id: 0
    };
  },

  methods: {
    rowDrop() {
      const tbody = document.querySelector('.el-table__body-wrapper tbody');
      const _this = this;
      Sortable.create(tbody, {
        forceFallback: true,
        dragClass: 'drag',
        delay: 100,
        animation: 1000,
        onEnd({ newIndex, oldIndex }) {
          //更换表格数据 后续拖动坐标才会正确
          let data = _this.$refs.table.getData();

          let nid = data[newIndex].id;
          let oid = data[oldIndex].id;
          const currRow = data.splice(oldIndex, 1)[0];
          data.splice(newIndex, 0, currRow);

          sortChange(data)
            .then((msg) => {})
            .catch((e) => {
              _this.$refs.table.reload();
            });
          //去数据库更换排序
        }
      });
    },
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return consumerLog({ ...where, ...order, page, limit });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    consumerLogRemove(row) {
      const loading = this.$loading({ lock: true });
      consumerLogRemove(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 一键默认 */
    doDefault() {
      this.$confirm(
        '请谨慎使用该功能，一旦使用则会变成系统默认内容。确定一键默认？',
        '提示',
        {
          type: 'warning'
        }
      )
        .then(() => {
          const loading = this.$loading({ lock: true });
          doDefault()
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          consumerLogRemove(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'status', value: row.status })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.status = row.status == 1 ? 2 : 1;
          this.$message.error(e.message);
        });
    },
    /* 更改状态 */
    editRecommend(row) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'is_recommend', value: row.is_recommend })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.is_recommend = row.is_recommend == 1 ? 2 : 1;
          this.$message.error(e.message);
        });
    }
  },
  mounted() {
    this.rowDrop();
  }
};
</script>

<style>
.drag {
  background: #000 !important;
  background-image: linear-gradient(#333, #999) !important;
}
</style>
